package application.model
{
	import application.event.SimpleModelEvent;
	import application.model.vo.LevelVO;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	/**
	 * ...
	 * @author Tiago Salomon
	 */
	public class LevelModel extends EventDispatcher
	{
		
		private var _levels:Vector.<LevelVO> = new Vector.<LevelVO>();
		
		public function LevelModel() 
		{
			//this._levels.push(new LevelVO(10, 1, [[1, 1, 1, 1, 0, 0, 0, 0], [1, 0, 0, 1, 1, 1, 1, 1], [1, 0, 2, 0, 0, 3, 0, 1], [1, 0, 3, 0, 0, 2, 4, 1], [1, 1, 1, 0, 0, 1, 1, 1], [0, 0, 1, 1, 1, 1, 0, 0]]));			
		}
		
		public function load():void 
		{
			var _urlLoader:URLLoader = new URLLoader();
				_urlLoader.addEventListener(Event.COMPLETE, completeLoadLevelsHandler);
				_urlLoader.load(new URLRequest("xml/Levels.xml"));
		}
		
		private function completeLoadLevelsHandler(e:Event):void 
		{
			var xmlList:XMLList = XML(e.currentTarget.data).level as XMLList;
			var rows:Array = []
			var cols:Array = [];
			for each (var level:XML in xmlList) 
			{									
				for each (var row:XML in level.row) 
				{					
					for each (var col:XML in row.col) 
					{
						cols.push(uint(col.@value));
					}
					rows.push(cols);
					
					cols = [];					
				}				
				this._levels.push(new LevelVO(level.@score, level.@dificulty, rows));
				rows = [];
			}
			
			this.dispatchEvent(new Event(Event.COMPLETE));			
		}
		
		public function get levels():Vector.<LevelVO> 
		{
			return _levels;
		}
		
	}

}